package com.cityfood.controller;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cityfood.common.pojo.CityFoodResult;
import com.cityfood.pojo.CfAdminUser;
import com.cityfood.service.AdminUserService;
import com.github.pagehelper.PageInfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 
 *操作管理员表
 */
@Controller
public class AdminUserController {

	//注入adminUserService接口实现类
	@Autowired
	private AdminUserService adminUserService;
	
	//调试日志
	public static final Logger logger = LoggerFactory.getLogger(AdminUserController.class);
	/**
	 * 验证管理员登陆
	 */
	@RequestMapping(value="/manage/adminUser/login",method=RequestMethod.POST)
	@ResponseBody
	public CityFoodResult login(@RequestBody Map<String,String> map,HttpServletRequest req,HttpServletResponse rsp) {
		
		// 获取登陆的用户名和密码
		String name = map.get("username");
		String pass = map.get("password");
		logger.info("username = " + name);
		// 根据用户名密码查找管理员
		CfAdminUser adminUser = adminUserService.getCfAdminUserByNameANDPass(name, pass);
		if (adminUser != null) {
			String remFlag = req.getParameter("rememberMe");
			if ("1".equals(remFlag)) {
				String loginInfo = name + "," + pass;
				// 创建Cookie
				Cookie userCookie = new Cookie("loginInfo", loginInfo);
				// 存活期为一天 1*24*60*60
				userCookie.setMaxAge(1 * 24 * 60 * 60);
				userCookie.setPath("/");
				// 存入Cookie
				rsp.addCookie(userCookie);
			}
			// 将管理员放到session里面，key为adminUser
			HttpSession session = req.getSession();
			session.setAttribute("adminUser", adminUser);
			return CityFoodResult.ok(true);
		}
		return CityFoodResult.ok(false);
	}
	
	
	//后台管理---------------------------------------------------------------------------
	
	/**
	 * 根据role查询管理员
	 */
	@RequestMapping(value="/manage/adminUser",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getAdminUserByCondition(@RequestParam(defaultValue="")String name,
											@RequestParam(defaultValue="")String phone,
											@RequestParam(defaultValue="100")Integer role,
											@RequestParam(defaultValue="1")Integer currentPage) {
		CityFoodResult cityfoodResult = new CityFoodResult();
		PageInfo<CfAdminUser> adminUserInfo = adminUserService.getAdminUserByCondition(name, phone, role, currentPage);
		if (adminUserInfo.getList().size()>0) {
			Map<String,Object> ms = new LinkedHashMap<>();
			ms.put("total", adminUserInfo.getTotal());
			ms.put("adminUsers", adminUserInfo.getList());
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("查询管理员成功");
			cityfoodResult.setData(ms);
		}else{
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("无管理员");
			cityfoodResult.setData("{}");
		}
		return cityfoodResult;
	}
	
	/**
	 * 根据id查询管理员
	 */
	@RequestMapping(value="/manage/adminUser/{id}",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getAdminUserById(@PathVariable Integer id) {
		CityFoodResult cityfoodResult = new CityFoodResult();
		CfAdminUser adminUser = adminUserService.getAdminUserById(id);
		if (adminUser!=null) {
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("根据管理员id查询管理员成功");
			cityfoodResult.setData(adminUser);
		}else{
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("根据管理员id查询管理员失败");
			cityfoodResult.setData("{}");
		}
		return cityfoodResult;
	}
	
	/**
	 * 添加管理员
	 */
	@RequestMapping(value="/manage/adminUser",method=RequestMethod.POST)
	@ResponseBody
	public CityFoodResult saveAdminUser(@RequestBody Map<String,Object> map) {
		
		CityFoodResult cityfoodResult = new CityFoodResult();
		
		String username = (String)map.get("username");
		String password = (String)map.get("password");
		String phone = (String)map.get("phone");
		String email = "";
		if (map.containsKey("email")) {
			email = (String)map.get("email");
		}
		
		String openid = "";
		if (map.containsKey("openid")) {
			openid = (String)map.get("openid");
		}
		
		Integer role = 3;
		if (map.containsKey("role")) {
			role = Integer.parseInt(map.get("role").toString());
		}
		
		CfAdminUser adminUser=new CfAdminUser();
		adminUser.setUsername(username);
		adminUser.setPassword(password);
		adminUser.setPhone(phone);
		adminUser.setEmail(email);
		adminUser.setOpenid(openid);
		adminUser.setCityId(1);
		adminUser.setRole(role.byteValue());
		CityFoodResult check = checkAdminUserByNameANDPhoneANDEmail(adminUser);
		if (check!=null) {
			return check;
		}
		int i = adminUserService.saveAdminUser(adminUser);
		if (i>0) {
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("添加管理员成功");
			cityfoodResult.setData(true);
		}else{
			cityfoodResult.setCode(0);
			cityfoodResult.setMsg("添加管理员失败");
			cityfoodResult.setData(false);
		}
		return cityfoodResult;
	}
	
	/**
	 * 根据管理员id修改管理员
	 */
	@RequestMapping(value="/manage/adminUser",method=RequestMethod.PUT)
	@ResponseBody
	public CityFoodResult updateAdminUserById(@RequestBody Map<String,Object> map) {
		
		CityFoodResult cityfoodResult = new CityFoodResult();
		
		Integer id = Integer.parseInt(map.get("id").toString());
		String username = (String)map.get("username");
		String password = (String)map.get("password");
		String phone = (String)map.get("phone");
		String email = "";
		if (map.containsKey("email")) {
			email = (String)map.get("email");
		}
		
		String openid = "";
		if (map.containsKey("openid")) {
			openid = (String)map.get("openid");
		}
		
		Integer role = 3;
		if (map.containsKey("role")) {
			role = Integer.parseInt(map.get("role").toString());
		}
		
		CfAdminUser adminUser=new CfAdminUser();
		adminUser.setId(id);
		adminUser.setUsername(username);
		adminUser.setPassword(password);
		adminUser.setPhone(phone);
		adminUser.setEmail(email);
		adminUser.setOpenid(openid);
		adminUser.setRole(role.byteValue());
		
		CityFoodResult check = checkAdminUserByNameANDPhoneANDEmail(adminUser);
		if (check!=null) {
			return check;
		}
		int i = adminUserService.updateAdminUserById(adminUser);
		if (i>0) {
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("根据管理员id修改管理员成功");
			cityfoodResult.setData(true);
		}else{
			cityfoodResult.setCode(0);
			cityfoodResult.setMsg("根据管理员id修改管理员失败");
			cityfoodResult.setData(false);
		}
		return cityfoodResult;
	}
	
	/**
	 * 根据管理员id删除管理员
	 */
	@RequestMapping(value="/manage/adminUser",method=RequestMethod.DELETE)
	@ResponseBody
	public CityFoodResult deleteAdminUserById(@RequestBody Map map){
	    String id = map.get("id").toString();
		CityFoodResult cityfoodResult = new CityFoodResult();
		int i = adminUserService.deleteAdminUserById(Integer.parseInt(id));
		if (i>0) {
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("根据管理员id删除管理员成功");
			cityfoodResult.setData(true);
		}else{
			cityfoodResult.setCode(0);
			cityfoodResult.setMsg("根据管理员id删除管理员失败");
			cityfoodResult.setData(false);
		}
		return cityfoodResult;
	}
	
	/**
	 * 根据管理员名称，电话和邮箱验证管理员是否存在
	 */
	public CityFoodResult checkAdminUserByNameANDPhoneANDEmail(CfAdminUser adminUser){
		CityFoodResult cityFoodResult = new CityFoodResult();
		if (adminUserService.getAdminUserByName(adminUser)!=null) {
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("管理员用户名已存在");
			cityFoodResult.setData("{}");
			return cityFoodResult;
		}
		if (adminUserService.getAdminUserByPhone(adminUser)!=null) {
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("管理员电话号码已存在");
			cityFoodResult.setData("{}");
			return cityFoodResult;
		}
		if (adminUserService.getAdminUserByEmail(adminUser)!=null) {
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("管理员邮箱已存在");
			cityFoodResult.setData("{}");
			return cityFoodResult;
		}
		return null;
	}

}
